name: Test

on:
  push:
  pull_request:

jobs:
  test:
    name: Run tests and linter
    strategy:
      matrix:
        os: [ubuntu-22.04]
        python: ["3.10", "3.9", "3.8"]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python }}

      - name: Setup poetry
        uses: Gr1N/setup-poetry@v8

      - name: Cache poetry virtualenv
        uses: actions/cache@v3
        id: poetry-cache
        with:
          path: ~/.cache/pypoetry/virtualenvs
          key: ${{ matrix.os }}-py${{ matrix.python }}-poetry-${{ hashFiles('**/poetry.lock') }}

      - name: Install libs for scipy
        run: sudo apt-get install gfortran libopenblas-dev liblapack-dev

      - name: Install dependencies
        run: poetry install
        if: steps.poetry-cache.outputs.cache-hit != 'true'

      - name: Test with pytest
        run: poetry run pytest -n 2 --cov-report xml --cov ./quantpiler

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3
        if: matrix.os == 'ubuntu-22.04' && matrix.python == '3.10'

      - name: Run linter
        run: poetry run black . --check
        if: matrix.os == 'ubuntu-22.04' && matrix.python == '3.10'
